package com.kmist.jianghu;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import TOOL.SaveTool;

public class JianHu extends Activity implements View.OnClickListener {

    public static SaveTool saveTool;
    public static Handler uiHandler;
    private Player player;
    private Monster monster;
    private LinearLayout lin;
    private LinearLayout jianghuRoot;
    private LinearLayout mainLayout;
    private LinearLayout LilianLayout;

    private TextView JH_C1_name;
    private ImageButton JH_C1_PlayerInfo;
    private Button JHBtn_jianghu;
    private Button JHBtn_lilian;
    private Button JHBtn_Bag;
    private Button Lilian_01_shenshuitan;
    private Button Lilian_02_gudongqu;

    /* 战斗界面*/
    private TextView playerName;
    private TextView playerHp;
    private TextView monsterHp;
    private TextView playerHpLine;
    private TextView monsterHpLine;
    private Combate com;

    int lineWidth;
    private RelativeLayout.LayoutParams playlp;
    private RelativeLayout.LayoutParams monlp;

    public JianHu() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b_jianghu_01);
        player = MainActivity.player;

        initViews();    //初始化控件
        handler_func();

        JH_C1_PlayerInfo.setOnClickListener(this);
        JHBtn_jianghu.setOnClickListener(this);
        JHBtn_lilian.setOnClickListener(this);
        JHBtn_Bag.setOnClickListener(this);

    }

    /* 收到信息更新UI */
    private void handler_func() {
        uiHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what){
                    //plarer attach
                    case 1:{
                        JianHu.this.monsterHp.setText(monster.getmCurHp() + "/" + monster.getmHp());
                        monlp.width = (int) (monster.getmCurHp()*1.0f / monster.getmHp()*1.0f * lineWidth);
                        monsterHpLine.setLayoutParams(monlp);

                        //得到LinearLayout
                        LinearLayout ll_content;
                        ll_content = (LinearLayout) findViewById(R.id.battle_moreindo);
                        //创建TextView
                        TextView textView = new TextView(JianHu.this);
                        textView.setTextColor(Color.WHITE);
                        //设置显示内容
                        String bateInfo = "<font color='red'>你</font> 使用了 <font color='yellow'>AA</font> 攻击，" + "<font color='blue'>" + monster.getmName()+ "</font>"+" 受到了 "
                                + "<font color='#9C27B0'>" + msg.arg1 + "</font>"+" 点伤害";
                        textView.setText(Html.fromHtml(bateInfo));
                        //添加到LinearLayout中
                        ll_content.addView(textView);

                    }break;
                    //monster attach
                    case 2:{
                        JianHu.this.playerHp.setText(player.getmCurHp() + "/" + player.getmHp());
                        playlp.width = (int) (player.getmCurHp()*1.0f / player.getmHp()*1.0f * lineWidth);
                        playerHpLine.setLayoutParams(playlp);

                        LinearLayout ll_content;
                        ll_content = (LinearLayout) findViewById(R.id.battle_moreindo);
                        //创建TextView
                        TextView textView = new TextView(JianHu.this);
                        textView.setTextColor(Color.WHITE);
                        String bateInfo = "<font color='blue'>" + monster.getmName() + "</font>"+ " 使用了 <font color='yellow'>撒娇</font> 攻击，<font color='red'>你</font> 受到了 "
                                + "<font color='#9C27B0'>" + msg.arg1 + "</font>"+"点伤害";

                        //设置显示内容
                        textView.setText(Html.fromHtml(bateInfo));
                        //添加到LinearLayout中
                        ll_content.addView(textView);

                    }
                    break;
                    case 9:{
                        LinearLayout ll_content;
                        ll_content = (LinearLayout) findViewById(R.id.battle_moreindo);
                        //创建TextView
                        TextView textView = new TextView(JianHu.this);
                        textView.setTextColor(Color.WHITE);
                        String bateInfo = null;
                        if(msg.arg1 == 1){
                            bateInfo = "<font color='red'> 你 </font>" + "击败了 " + "<font color='blue'>" + monster.getmName() + "</font>" +"\n"
                            + "获得了 <front color='#FBC02D'>" + msg.arg2 + "</fong> 金币";

                        }else if(msg.arg1 == 2){
                            bateInfo = "<font color='red'> 你 </font>"+ "被 " + "<font color='blue'>" + monster.getmName() + "</font>" + " 击败了。";
                        }
                        textView.setText(Html.fromHtml(bateInfo));
                        ll_content.addView(textView);
                        //setContentView(R.layout.jianghu);
                    }
                    break;
                }
                return true;
            }
        });
    }

    /**
     * 关联控件，初始化列表
     */
    private void initViews() {

        lin = (LinearLayout) findViewById(R.id.jianghu_01_dynamicbar);
        jianghuRoot = (LinearLayout) findViewById(R.id.jianghu_root);
        mainLayout = (LinearLayout) findViewById(R.id.jianghu_dynamic);
        LilianLayout = (LinearLayout) findViewById(R.id.jianghu_c2_lilian);

        JH_C1_name = findViewById(R.id.property_text_name);
        JH_C1_PlayerInfo = (ImageButton) findViewById(R.id.property_imagebtn_head);
        JHBtn_jianghu = findViewById(R.id.menubar_btn01_jianghu);
        JHBtn_lilian = findViewById(R.id.menubar_btn02_lilian);
        JHBtn_Bag = findViewById(R.id.menubar_btn03_bag);

        /* 动态布局 */
        final LayoutInflater inflater = LayoutInflater.from(this);

        /* 动态布局初始化 */
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.b_jianghu_02_index, null).findViewById(R.id.jianghu_dynamic);
        lin.addView(layout);
        JH_C1_name.setText(player.getmName());
    }

        @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.menubar_btn01_jianghu:{
                //切换到主界面
                loadMain_Card();
            }
            break;
            case R.id.menubar_btn02_lilian:{
                //切换到历练界面
                loadLilian_Card();
            }
            break;
            case R.id.property_imagebtn_head:{
                //切换到玩家属性界面
                loadPlayerinfo_Page();
            }
            break;
            case R.id.lilian_01_shenshuitan:{
                startLilian(00);
            }
            break;
            case R.id.lilian_02_gudongqu:{
                startLilian(01);
            }

            /* attach */
            case R.id.attack1:{
                com.attackMonser(1);
            }break;
            case R.id.attack2:{
                com.attackMonser(2);
            }break;
            case R.id.attack3:{
                com.attackMonser(3);
            }break;
            case R.id.attack4:
            {
                com.attackMonser(4);
            }
            break;
        }
    }
    private void loadMain_Card(){
        LayoutInflater mBaseInflater = LayoutInflater.from(this);     //基本释义LayoutInflater实例
        LinearLayout MainLayout = (LinearLayout) mBaseInflater.inflate(R.layout.b_jianghu_02_index, null);
        lin.removeAllViews();
        lin.addView(MainLayout);
    }

    private void loadLilian_Card(){
        LayoutInflater mBaseInflater = LayoutInflater.from(this);     //基本释义LayoutInflater实例
        LinearLayout baseLayout = (LinearLayout) mBaseInflater.inflate(R.layout.jianghu_c2_lilian, null);
        //获取控件ID
        Lilian_01_shenshuitan = baseLayout.findViewById(R.id.lilian_01_shenshuitan);
        Lilian_02_gudongqu = baseLayout.findViewById(R.id.lilian_02_gudongqu);
        //设置监听
        Lilian_01_shenshuitan.setOnClickListener(this);
        Lilian_02_gudongqu.setOnClickListener(this);
        //切换布局

        lin.removeAllViews();
        lin.addView(baseLayout);
    }

    private void loadPlayerinfo_Page(){
        setContentView(R.layout.player_attribute);
    }

    private void startLilian(int area){

        setContentView(R.layout.combate);
        //获取控件ID
        playerName = (TextView) findViewById(R.id.combate_playerName);
        TextView monsterName = (TextView) findViewById(R.id.combate_monsterName);
        playerHp = (TextView) findViewById(R.id.player_hp_value);
        monsterHp = (TextView) findViewById(R.id.monster_hp_value);
        playerHpLine = (TextView) findViewById(R.id.combate_playerHp);
        monsterHpLine = (TextView) findViewById(R.id.combate_monsterHp);
        Button attack1 = (Button) findViewById(R.id.attack1);
        Button attack2 = (Button) findViewById(R.id.attack2);
        Button attack3 = (Button) findViewById(R.id.attack3);
        Button attack4 = (Button) findViewById(R.id.attack4);

        attack1.setOnClickListener(this);
        attack2.setOnClickListener(this);
        attack3.setOnClickListener(this);
        attack4.setOnClickListener(this);

        /* 动态血条 */
        playlp = (RelativeLayout.LayoutParams) playerHpLine.getLayoutParams();
        monlp = (RelativeLayout.LayoutParams) monsterHpLine.getLayoutParams();

        lineWidth = 516;

        //lineWidth = playerHpLine.getWidth();
        System.out.println("****" + lineWidth);
        com = new Combate(area);
        monster = com.getMonster();
        playerName.setText(player.getmName());
        monsterName.setText(monster.getmName());

        playerHp.setText(player.getmCurHp() + "/" + player.getmHp());
        monsterHp.setText(monster.getmCurHp() + "/" + monster.getmHp());
        com.start();
    }
}
